package com.example.xianji.adapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import com.example.xianji.activity.Main_BaiKe_DetailActivity;
import com.example.xianji.R;
import com.example.xianji.bean.BaiKe_List;
import com.example.xianji.utils.ImageLoadUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.imageaware.ImageViewAware;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/********************百科适配器************************************/
public class BaiKe_Adapter  extends BaseAdapter{
	private Context mContext;
	private ArrayList<BaiKe_List> list;
	private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
	
	private   DisplayImageOptions options;
	public BaiKe_Adapter(Context mContext, ArrayList<BaiKe_List> list) {
		super();
		this.mContext = mContext;
		this.list = list;
		options = new DisplayImageOptions.Builder()
		/*.showImageOnLoading(R.drawable.touxiang)
		.showImageForEmptyUri(R.drawable.touxiang)
		.showImageOnFail(R.drawable.touxiang)*/
		.bitmapConfig(Bitmap.Config.RGB_565)//设置为RGB565比起默认的ARGB_8888要节省大量的内存
		.delayBeforeLoading(100)//载入图片前稍做延时可以提高整体滑动的流畅度
		.cacheInMemory(true)
		.cacheOnDisk(true)
		.considerExifParams(true)
		.displayer(new RoundedBitmapDisplayer(10)).build();
	}

	
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder mHolder=null;
		if(convertView==null){
			mHolder=new Holder();
			convertView=View.inflate(mContext, R.layout.baike_item, null);
			mHolder.baike_image=(ImageView) convertView.findViewById(R.id.baike_image);
			mHolder.baike_title= (TextView) convertView.findViewById(R.id.baike_title);
			mHolder.baike_details= (TextView) convertView.findViewById(R.id.baike_details);
			convertView.setTag(mHolder);
		}
		mHolder=(Holder)convertView.getTag();
	final	int a=position;
		
		convertView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent=new Intent(mContext, Main_BaiKe_DetailActivity.class);
				intent.putExtra("baike_detail", list.get(a).getId());
				mContext.startActivity(intent);
			}
		});
		mHolder.baike_title.setText(list.get(position).getWord_name());
		mHolder.baike_details.setText(list.get(position).getSummary());
		//显式的将checkActualViewSize设为false, 这样图片的缓存也将只会保存一个副本,保证第二次查询时可以直接命中
	ImageViewAware imagevieware=new ImageViewAware( mHolder.baike_image, false);
		ImageLoadUtil.loadImageByPath(options, list.get(position).getPic(),imagevieware, animateFirstListener);
		//ImageLoader.getInstance().displayImage(, , , );
		return convertView;
	}
class Holder{
	ImageView baike_image;
	TextView baike_title,baike_details;
}

public static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {
	public static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

	@Override
	public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
		if (loadedImage != null) {
			ImageView imageView = (ImageView) view;
			boolean firstDisplay = !displayedImages.contains(imageUri);
			if (firstDisplay) {
				FadeInBitmapDisplayer.animate(imageView, 200);
				displayedImages.add(imageUri);
			}
		}
	}
	
}
}
